package com.bamboo.custom.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.bamboo.custom.R;

/**
 * 项目名称：CustomViewExercise
 *
 * @Author bamboolife
 * 邮箱：core_it@163.com
 * 创建时间：2021/9/12 6:59 下午
 * 描述：
 */
public class StatisticsView extends View{
    private Context mContext;
    /** 背景画笔 **/
    private Paint mBgPaint;

    /** 画笔 **/
    private Paint mPaint;

    private int mMaxWidth;
    private int mMaxHeight;

    /** 颜色 **/
    private int selectColor = 0;
    /** 值 **/
    private int value = 0;

    public StatisticsView(Context context) {
        super(context,null);
        mContext = context;
    }

    public StatisticsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs,0);
        mContext = context;
        TypedArray ta = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.StatisticsView, 0, 0);
        if (ta!=null){
            selectColor = ta.getColor(R.styleable.StatisticsView_select_color,0);
            value = ta.getInt(R.styleable.StatisticsView_value,0);
        }
        ta.recycle();
        init();
    }

    public StatisticsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mMaxWidth = View.MeasureSpec.getSize(widthMeasureSpec);
        mMaxHeight = View.MeasureSpec.getSize(heightMeasureSpec);
    }

    private void init(){
        mBgPaint = new Paint();
        mBgPaint.setStyle(Paint.Style.FILL);
        mBgPaint.setAntiAlias(true);
        //背景颜色（自己定义）
        mBgPaint.setColor(ContextCompat.getColor(mContext, R.color.design_default_color_secondary));

        mPaint = new Paint();
        mPaint.setColor(selectColor);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        deawBG(canvas);
        deawValue(canvas);
    }

    /**
     * 画背景
     */
    private void deawBG(Canvas canvas){
        if (mBgPaint==null){
            return;
        }
        RectF rectF = new RectF();
        rectF.top=0;
        rectF.left=0;
        rectF.bottom=mMaxHeight;
        rectF.right=mMaxWidth;
        canvas.drawRoundRect(rectF,mMaxWidth/2,mMaxWidth/2,mBgPaint);
    }

    /**
     * 画值
     */
    private void deawValue(Canvas canvas){
        if (mPaint==null){
            return;
        }
        RectF rectF = new RectF();
        rectF.left=0;
        rectF.top=mMaxHeight/100*(100 - value);
        rectF.bottom=mMaxHeight;
        rectF.right=mMaxWidth;
        canvas.drawRoundRect(rectF,mMaxWidth/2,mMaxWidth/2,mPaint);
    }

    public void setData(int value){
        this.value = value;
        invalidate();
    }
    public void setColor(int color){
        this.selectColor = color;
        invalidate();
    }
}
